package com.inossem.utils.base;

import android.content.Context;
import android.text.TextUtils;

import com.inossem.utils.R;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.exception.InossemException;

/**
 * Created by leij on 2017/9/30.
 */

public class QRCodeUtils {

    public static String DecryptQRCode(String qrCode, int... parameterType) {
        String result;
        String qrCodeType = getQRCodeType(qrCode);
//        LogUtils.i("qrCodeType:" + qrCodeType);
        switch (qrCodeType) {
            case QRCodeConstant.TYPE_BATCH:
                if (parameterType.length != 1) {
                    throw new InossemException("when type is " + QRCodeConstant.TYPE_BATCH + ", parameterType must not null and only one");
                }
                String[] qrCodeArray = getQRCodeRealInfo(qrCode).split(QRCodeConstant.DELIMITER);
//                for (int i = 0; i < qrCodeArray.length; i++) {
//                    LogUtils.e("index:" + i + "---qrCodeInfo:" + qrCodeArray[i]);
//                }
                result = qrCodeArray[parameterType[0]];
                break;
            case QRCodeConstant.TYPE_MATERIEL_CODE:
                result = getQRCodeRealInfo(qrCode);
                break;
            case QRCodeConstant.TYPE_PURCHASE_ORDER:
                result = getQRCodeRealInfo(qrCode);
                break;
            case QRCodeConstant.TYPE_POSITION:
                result = getQRCodeRealInfo(qrCode);
                break;
            default:
                result = "";
                break;
        }
//        LogUtils.i("qrCodeResult:" + result);
        return result;
    }

//    public static String decryptQRCodeWithType(String qrCode, String type, int... parameterType) {
//        String result;
//        switch (type) {
//            case QRCodeConstant.TYPE_BATCH:
//                if (parameterType.length != 1) {
//                    throw new InossemException("when type is " + QRCodeConstant.TYPE_BATCH + ", parameterType must not null and only one");
//                }
//                String[] qrCodeArray = getQRCodeRealInfo(qrCode).split(QRCodeConstant.DELIMITER);
//                result = qrCodeArray[parameterType[0]];
//                break;
//            default:
//                result = getQRCodeRealInfo(qrCode);
//                break;
//        }
//        return result;
//    }

    public static String getQRCodeType(String qrCode) {
        return qrCode.substring(0, QRCodeConstant.QRCODE_TYPE_LENGTH);
    }

    private static String getQRCodeRealInfo(String qrCode) {
        return qrCode.substring(QRCodeConstant.QRCODE_TYPE_LENGTH, qrCode.length());
    }

    public static String checkQRCode(Context context, String qrCode) {
        boolean flag0 = TextUtils.isEmpty(qrCode);
        if (flag0) {
            return context.getResources().getString(R.string.utils_qrcode_illegal_null);
        }

        boolean flag1 = qrCode.startsWith(QRCodeConstant.QRCODE_PREFIX);
        if (!flag1) {
            return context.getResources().getString(R.string.utils_qrcode_illegal_prefix);
        }

        boolean flag2 = qrCode.length() >= (QRCodeConstant.QRCODE_TYPE_LENGTH + QRCodeConstant.QRCODE_SECURITY.length());
        if (!flag2) {
            return context.getResources().getString(R.string.utils_qrcode_illegal_length);
        }

        boolean flag3 = qrCode.endsWith(QRCodeConstant.QRCODE_SECURITY);
        if (!flag3) {
            return context.getResources().getString(R.string.utils_qrcode_illegal_suffix);
        }

        boolean flag4 = true;
        if (qrCode.startsWith(QRCodeConstant.TYPE_BATCH)) {
            if (qrCode.split(QRCodeConstant.DELIMITER).length == QRCodeConstant.ARRAY_NUMBER_TYPE_BATCH) {
                flag4 = true;
            } else {
                flag4 = false;
            }
        }
        if (!flag4) {
            return context.getResources().getString(R.string.utils_qrcode_illegal_content);
        }

        return null;
    }

    public static boolean checkQRCodeType(String qrCode, String... qrCodeTypeArray) {
        String currentQRCodeType = getQRCodeType(qrCode);
        for (int i = 0; i < qrCodeTypeArray.length; i++) {
            if (qrCodeTypeArray[i].equals(currentQRCodeType)) {
                return true;
            }
        }
        return false;
    }


}
